package com.example.service.impl;


import com.example.mapper.DeptLogMapper;
import com.example.mapper.DeptMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.Dept;
import com.example.pojo.DeptLog;
import com.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private DeptLogMapper deptLogMapper;


    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

//    @Transactional(rollbackFor = Exception.class) // spring 事务管理   rollbackFor:所有异常都回滚
    @Transactional
    @Override
    public void delete(Integer id) {
        try {
            deptMapper.delect(id); // 根据id删除部门
            empMapper.delectDeptByid(id); // 根据部门id删除该部门下的员工信息
        }finally {
            DeptLog log = new DeptLog();
            log.setCreateTime(LocalDateTime.now());
            log.setDesctiption("解散部门id:" + id);
            deptLogMapper.insert(log);
        }
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public List<Dept> select(Integer id) {
        return deptMapper.select(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }


}
